import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/UiUtil.dart';
import 'package:sqflite/sqflite.dart';

import '../../pojo/ImPojo.dart';
import '../../service/ImMessageService.dart';
import '../../util/ImSingleton.dart';

abstract class SessionSettingBase extends StatelessWidget {

  const SessionSettingBase({super.key});

  bool isTop(ImSession session) {
    if (session.groupCode != null) {
      return session.group?.currentGroupUser?.isTop??false;
    } else {
      return session.fromUserId!.compareTo(session.toUserId!) >= 0 ? session.isTop : session.isTop2;
    }
  }

  bool isDisturb(ImSession session) {
    if (session.groupCode != null) {
      return session.group?.currentGroupUser?.isDisturb??true;
    } else {
      return session.fromUserId!.compareTo(session.toUserId!) >= 0 ? session.isDisturb : session.isDisturb2;
    }
  }

  bool isWaking(ImSession session) {
    if (session.groupCode != null) {
      return session.group?.currentGroupUser?.isWaking??false;
    } else {
      return session.fromUserId!.compareTo(session.toUserId!) >= 0 ? session.isWaking : session.isWaking2;
    }
  }

  void cleanMessages(BuildContext context, ImSession session) {
    showDialog(context: context,
        builder: (context) {
          return AlertDialog(title: const Text('提醒', style: TextStyle(color: Colors.blueAccent)),
              content: const Text('确定要清空聊天记录？'),
              actions: <Widget>[
                TextButton(child: const Text("确定"), onPressed: () {
                  _cleanMessages(context, session);
                }),
                TextButton(child: const Text("取消"), onPressed: () => Navigator.of(context).pop())
              ]);
        });

  }

  void _cleanMessages(BuildContext context, ImSession session) async {
    String userId = ImSingleton.getInstance().session.userId!;
    int sessionId = session.id!;
    var messageService = new ImMessageService();
    await messageService.cleanMessage(userId, sessionId);

    UiUtil.uiCleanMessages();

    UiUtil.showSnackBar(context, '清空消息完成！');
    Navigator.of(context).pop();
  }
}


Future<void> cleanMessage(Map<String, Object> paramMap) async {
  String userId = paramMap['userId'] as String;
  int sessionId = paramMap['sessionId'] as int;
  Database dataBase = paramMap['dataBase'] as Database;
  // ImSingleton.getInstance().dataBase = dataBase;
  var messageService = ImMessageService();
  messageService.cleanMessage(userId, sessionId);
}